package com.huaxin.hxcloud.work;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huaxin.common.Entity.SignInfoDTO;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author songxf
 *         created at 2017/7/14 9:56
 * @desc 签到 添加描述 工作事项  adapter
 */

class HxMatterRecycleAdapter extends RecyclerView.Adapter<HxMatterRecycleAdapter.ViewHolder> {
    private static final String TAG = "HxMatterRecycleAdapter";
    private       Context                         mContext;
    private       List<SignInfoDTO.SignTypesBean> mBeanData;
    private final LayoutInflater                  mInflater;

    public HxMatterRecycleAdapter(Context context, List<SignInfoDTO.SignTypesBean> data) {
        this.mContext = context;
        this.mBeanData = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_sign_type_des, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final SignInfoDTO.SignTypesBean typesBean = mBeanData.get(position);
        holder.mItemTypeTv.setSelected(typesBean.isCheck());
        holder.mItemTypeTv.setText(typesBean.getDes());
        holder.mItemTypeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                typesBean.setCheck(!typesBean.isCheck());
                notifyDataSetChanged();
                mOnItemClickListener.OnItemClickListener(selectMatters());
            }
        });
    }

    @Override
    public int getItemCount() {
        return (mBeanData==null||mBeanData.size() == 0) ? 0 : mBeanData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_type_tv)
        TextView mItemTypeTv;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void OnItemClickListener(List<String> matters);
    }

    public void setItemOnClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 获取选择工作事项的集合
     *
     * @return
     */
    public List<String> selectMatters() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < mBeanData.size(); i++) {
            if (mBeanData.get(i).isCheck()) {
                data.add(mBeanData.get(i).getId() + "");
            }
        }
        return data;
    }


}
